#include "ui_animated_dialog.h"
#include "ui_ui_animated_dialog.h"

UiAnimatedDialog::UiAnimatedDialog(bool need_shake, int play_mode, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UiAnimatedDialog),need_shake_(need_shake),gif_play_mode_(play_mode)
{
    ui->setupUi(this);
    this->setProperty("StyleSheet","Dialog");
    ui->textBrowser->setReadOnly(true);
    ui->textBrowser->setTextInteractionFlags(Qt::NoTextInteraction);
    ui->textBrowser->setProperty("StyleSheet","TextBrowser");

    type_thread_ = nullptr;

    if(need_shake_)
    {
        shake_timer_ = new QTimer(this);
        connect(shake_timer_,SIGNAL(timeout()),this,SLOT(slot_timeout()));
        position_ = 0;
    }
}

UiAnimatedDialog::~UiAnimatedDialog()
{
    if(type_thread_ != nullptr && type_thread_->isRunning())
    {
        type_thread_->Stop();
        type_thread_->wait();
    }
    delete type_thread_;
    type_thread_ = nullptr;

    delete ui;
}

void UiAnimatedDialog::SetAvatarType(const int &type)
{
    ui->avater_widget->SetAvatarType(type);
    ui->avater_widget->SetGifPlayTimes(gif_play_mode_);
}

void UiAnimatedDialog::SetAvatarTypeCustom(QMap<QString, QString> res, bool is_gif)
{
    ui->avater_widget->SetAvatarTypeCustom(res, is_gif);
}

void UiAnimatedDialog::ShakeWidget()
{
    if(need_shake_)
    {
        current_pos_ = this->pos();
        shake_timer_->setInterval(40);
        shake_timer_->start();
    }
}

void UiAnimatedDialog::Show(const QString &avatar_status, bool typewriter_mode, bool shake)
{
    ui->textBrowser->clear();

    if(typewriter_mode && (!content_text_.isEmpty()))
    {
        type_thread_ = new ThreadTypeWriter;
        connect(type_thread_,SIGNAL(signal_put_char(QChar)),this,SLOT(slot_get_char(QChar)));
        connect(type_thread_,SIGNAL(finished()),this,SLOT(slot_type_finished()));
        type_thread_->SetParam(content_text_);
        type_thread_->start(QThread::HighPriority);
    }
    else if(!typewriter_mode)
    {
        ui->textBrowser->setText(content_text_);
    }

    if(!avatar_status.isEmpty())
    {
        ui->avater_widget->SetMovie(avatar_status);
    }

    show();
    if(shake)
    {
        ShakeWidget();
    }
}

int UiAnimatedDialog::Exec(const QString &avatar_status, bool typewriter_mode, bool shake)
{
    ui->textBrowser->clear();

    if(typewriter_mode && (!content_text_.isEmpty()))
    {
        type_thread_ = new ThreadTypeWriter;
        connect(type_thread_,SIGNAL(signal_put_char(QChar)),this,SLOT(slot_get_char(QChar)));
        connect(type_thread_,SIGNAL(finished()),this,SLOT(slot_type_finished()));

        type_thread_->SetParam(content_text_);
        type_thread_->start();
    }
    else if(!typewriter_mode)
    {
        ui->textBrowser->setText(content_text_);
    }

    if(!avatar_status.isEmpty())
    {
        ui->avater_widget->SetMovie(avatar_status);
    }

    show();
    if(shake)
    {
        ShakeWidget();
    }

    return exec();
}

UiAnimatedDialog* UiAnimatedDialog::CreateBasicDialog(bool need_shake, int play_mode, const int &avatar_type, QWidget *parent)
{
    UiAnimatedDialog* dlg = new UiAnimatedDialog(need_shake, play_mode, parent);
    dlg->setAttribute(Qt::WA_DeleteOnClose);
    dlg->SetAvatarType(avatar_type);

    return dlg;
}

UiAnimatedDialog* UiAnimatedDialog::CreateCustomDialog(const QString &title, const QString &content, const QMap<QString, QString> res, bool need_shake, int play_mode, bool is_gif, QWidget *parent)
{
    UiAnimatedDialog* dlg = CreateBasicDialog(need_shake, play_mode, UiAvatarWidget::ENUM_AVATAR_CUSTOM, parent);
    dlg->setWindowTitle(title);
    dlg->SetText(content);
    dlg->SetAvatarTypeCustom(res,is_gif);

    return dlg;
}

UiAnimatedDialog* UiAnimatedDialog::CreateEvilDialog(const QString &title, const QString &content, const QMap<QString, QString> res, bool need_shake, int play_mode, bool is_gif, QWidget *parent)
{
    UiAnimatedDialog* dlg = CreateBasicDialog(need_shake, play_mode, UiAvatarWidget::ENUM_AVATAR_EVIL, parent);
    dlg->setWindowTitle(title);
    dlg->SetText(content);
    dlg->SetAvatarTypeCustom(res,is_gif);

    return dlg;
}

UiAnimatedDialog* UiAnimatedDialog::CreateAngelDialog(const QString &title, const QString &content, const QMap<QString, QString> res, bool need_shake, int play_mode, bool is_gif, QWidget *parent)
{
    UiAnimatedDialog* dlg = CreateBasicDialog(need_shake, play_mode, UiAvatarWidget::ENUM_AVATAR_ANGEL, parent);
    dlg->setWindowTitle(title);
    dlg->SetText(content);
    dlg->SetAvatarTypeCustom(res,is_gif);

    return dlg;
}

UiAnimatedDialog* UiAnimatedDialog::CreateActorDialog(const QString &title, const QString &content, const QMap<QString, QString> res, bool need_shake, int play_mode, bool is_gif, QWidget *parent)
{
    UiAnimatedDialog* dlg = CreateBasicDialog(need_shake, play_mode, UiAvatarWidget::ENUM_AVATAR_ACTOR, parent);
    dlg->setWindowTitle(title);
    dlg->SetText(content);
    dlg->SetAvatarTypeCustom(res,is_gif);

    return dlg;
}

void UiAnimatedDialog::slot_timeout()
{
    shake_timer_->stop();
    if(position_ < const_max_times)
    {
        ++position_;
        switch(position_%4)
        {
        case 1:
        {
            QPoint tmpPos(current_pos_.x(),current_pos_.y()-const_max_space);
            this->move(tmpPos);
        }
            break;
        case 2:
        {
            QPoint tmpPos(current_pos_.x()-const_max_space,current_pos_.y()-const_max_space);
            this->move(tmpPos);
        }
            break;
        case 3:
        {
            QPoint tmpPos(current_pos_.x()-const_max_space,current_pos_.y());
            this->move(tmpPos);
        }
            break;
        default:
        case 0:
            this->move(current_pos_);
            break;
        }
        shake_timer_->start();
    }
}

void UiAnimatedDialog::slot_get_char(QChar disp_char)
{
    //强调颜色代码
    /*QTextCharFormat tcf;
    tcf.setForeground(QColor(255,0,0));
    ui->textBrowser->mergeCurrentCharFormat(tcf);*/
    ui->textBrowser->insertPlainText(disp_char);
}

void UiAnimatedDialog::slot_type_finished()
{

}
